<%@page import="java.text.DecimalFormat"%>
<%@page import="java.math.BigDecimal"%>
<%@page import="java.text.DecimalFormat"%>
<%@page import="java.text.DecimalFormatSymbols"%>
<%@page import="java.text.NumberFormat"%>
<%@page import="java.util.Locale"%>

<%@page import="extend.org.compiere.utils.Util"%>
<%@page import="org.compiere.dsr.DSR_Cell"%>
<%@page import="org.compiere.model.MBPMFormCode"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>

<%@ taglib prefix="s" uri="/struts-tags"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%@ page
	import="main.org.form.action.FormBP, main.org.form.action.Forms, main.org.model.*, org.compiere.model.*, org.compiere.util.Env"%>
<%@ page import="org.compiere.dsr.DSR_DataCollection"%>

<%
	FormBP fBP = (FormBP) session.getAttribute("formBP");
	DSR_DataCollection collection = fBP.getCollection();
	String formName = new MBPMForm(Env.getCtx(), fBP.getBPM_Form_ID(),
			null).getName();

	int rowSpan = collection.size();
	int rowC = 1;
	int colC = 0;
	String cellStyle = "";
	boolean cellChanged = false;
%>
<s:set var="bReadOnly"><%=fBP.getReadOnly()%></s:set>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Ввод данных по формам БО для ДЗО</title>
<link rel="shortcut icon" href="images/logo.png" type="image/jpg" />
<link type="text/css" href="css/Main.css" rel="stylesheet">
<%@ include file="/js/JScript.jsp"%>
</head>
<body>
	<div>
		<div id="back" class="letter">
			<a class="aBack" title="Вернуться назад"
				href="<s:url action='forms'>
				<s:param name="AD_Client_ID"><%=fBP.getAD_Client_ID()%></s:param>
				<s:param name="AD_Org_ID"><%=fBP.getAD_Org_ID()%></s:param>
				<s:param name="BPM_VersionBudget_ID"><%=fBP.getBPM_VersionBudget_ID()%></s:param>
				<s:param name="BPM_Project_ID"><%=fBP.getBPM_Project_ID()%></s:param>
				<s:param name="AD_User_ID"><%=fBP.getAD_User_ID()%></s:param>
			</s:url>">
				<img src="images/back.png" />
			</a>
		</div>
		<s:url id="excelDownload" namespace="/" action="DownloadBP">
			<s:param name="page">
				<s:property value="page" />
			</s:param>
			<s:param name="BPM_Project_ID"><%=fBP.getBPM_Project_ID()%></s:param>
			<s:param name="AD_Org_ID"><%=fBP.getAD_Org_ID()%></s:param>
			<s:param name="BPM_Form_ID"><%=fBP.getBPM_Form_ID()%></s:param>
			<s:param name="AD_User_ID"><%=fBP.getAD_User_ID()%></s:param>
			<s:param name="AD_Client_ID"><%=fBP.getAD_Client_ID()%></s:param>
		</s:url>
		<div id="printExcel" class="letterRight letter">
			<s:a href="%{excelDownload}" title="Выгрузка в Excel">
				<img src="images/excel.png" style="margin-left: 10px;" />
			</s:a>
			<img src="images/separator.png" style="margin-left: 5px;" /> <a
				href='javascript:window.print(); void 0;' title="Печать"><img
				src="images/print.png" style="margin-left: 5px;" /></a>
		</div>
		<table width="100%" cellspacing="2" align="center">
			<tr>
				<td scope="col"><font color="#321818"
					style="font-family: sans-serif; font-style: italic" size="+2">Ввод
						данных по бюджетным формам</font> <br />&nbsp;</td>
			</tr>
		</table>

		<%
			MBPMProject project = null;
			FormBP fPropertyBP = (FormBP) session.getAttribute("formBP");
			String orgName = "";
			if (fPropertyBP != null) {
				project = new MBPMProject(Env.getCtx(),
						fPropertyBP.getBPM_Project_ID(), null);
				if (fPropertyBP.getAD_Org_ID() == 0)
					orgName = "Холдинг Зерде";
				else
					orgName = new MOrg(Env.getCtx(),
							fPropertyBP.getAD_Org_ID(), null).getName();
			}
		%>

		<body>
			<table width="60%" cellspacing="2" style="margin-left: 150px;">
				<tr>
					<td width="20%"
						style="border: double; border-color: #8DB3DC; text-align: left; padding-left: 10px;">
						<font color="#321818"
						style="font-family: sans-serif; font-style: normal;" size="+1">Организация</font>
					</td>
					<td
						style="border: dotted; border-color: #999999; width: 40%; text-align: left; padding-left: 10px;">
						<b><%=orgName%></b>
					</td>
				</tr>
				<tr>
					<td
						style="border: double; border-color: #8DB3DC; text-align: left; padding-left: 10px;">
						<font color="#321818"
						style="font-family: sans-serif; font-style: normal;" size="+1">Рабочий
							проект</font>
					</td>
					<td
						style="border: dotted; border-color: #999999; text-align: left; padding-left: 10px;">
						<b><%=project.getName()%></b>
					</td>
				</tr>
			</table>


			<table width="60%" cellspacing="2" style="margin-left: 150px;">
				<tr>
					<td width="20%"
						style="border: double; border-color: #F18800; text-align: left; padding-left: 10px;">
						<font color="#321818"
						style="font-family: sans-serif; font-style: normal;" size="+1">Форма</font>
					</td>
					<td
						style="border: dotted; border-color: #999999; width: 40%; text-align: left; padding-left: 10px;">
						<b><%=formName%></b>
					</td>
				</tr>
			</table>
			<s:form id="myFormBP">
				<table class="cursorHover more" border="1" style="margin-top: 20px;"
					bordercolor="#663300" align="center" cellspacing="2" id="tableBP">
					<tr class="trLightBlue">
						<%
							for (int i = 0; i < collection.RowHeader.size(); i++) {
						%>
						<th><%=collection.RowHeader.get(i)%></th>
						<%
							}
						%>
						<%
							DSR_Cell cell = null;
								for (int h = 1; h < collection.getHeader().size(); h++) {
									cell = collection.getHeader().getCell(h);
									String formula = cell.getFormula();
						%>
						<s:set var="formula"><%=cell.getFormula()%></s:set>

						<th scope="col" id="column<%=h%>" title="<%=formula%>">
							<s:if test="%{#formula!='null'}">
								<input id="checkbox<%=h%>" type="checkbox"
									name="checkbox<%=h%>"
									onchange='hideunhide("checkbox<%=h%>", "<%=formula%>",<%=collection.RowHeader.size()%>);' "/>
							</s:if> <%=cell.getValue()%>
						</th>
						<%
							}
						%>
					</tr>
					<%
						for (int i = 0; i < collection.size(); i++) {
					%>
					<tr>
						<%
							colC = 0;
									for (int j = 0; j < collection.getRow(i).size(); j++) {
										cell = collection.getRow(i).getCell(j);
						%>
						<%
							String value = cell.getValue();
										if (value.length() > 1) {
											if (value.substring(0, 2).equals("00")) {

											} else {
												try {

													Double.parseDouble(value);
													BigDecimal bd = new BigDecimal(
															cell.getValue());
													DecimalFormat formatter = (DecimalFormat) NumberFormat
															.getInstance(Locale.getDefault());
													DecimalFormatSymbols symbols = formatter
															.getDecimalFormatSymbols();

													symbols.setGroupingSeparator(' ');
													formatter.setDecimalFormatSymbols(symbols);
													value = formatter.format(bd.longValue());

												} catch (Exception e) {

												}
											}
										}
						%>

						<s:set var="cellValue"><%=value%></s:set>
						<s:set var="col"><%=colC%></s:set>
						<s:set var="row"><%=rowC%></s:set>
						<s:set var="formula"><%=cell.getFormula()%></s:set>
						<%
							if (cell.isRow) {
											for (int k = 0; k < collection.RowHeader.size(); k++) {
												String sRow = "";
												int colspan = 0;
						%>
						<%
							if ((k == 0) && (collection.isOrderBy))
													sRow = cell.RowNumber;
												if (k == cell.LevelIndex) {
													sRow = cell.getValue();
													colspan = collection.RowHeader.size()
															- cell.LevelIndex;
												}
						%>
						<td class="tdRow" colspan=<%=colspan%>><%=sRow%></td>
						<%
							if (colspan > 0)
													break;
											}
										} else {
						%>

						<td bgcolor="#8DB3DC" id="td_Row<%=rowC%>_Col<%=colC%>">
							<s:if test="#formula == 'null'">
								<s:set var="readonly" value="'false'" />
							</s:if> <s:else>
								<s:set var="readonly" value="'true'" />
							</s:else> <s:if test="#bReadOnly == 'true'">
								<s:set var="readonly" value="'true'" />
							</s:if> <%
 	cellChanged = cell.Changed;
 					if (cell.Changed) {
 						cellStyle = "changedValue";
 					} else {
 						cellStyle = "";
 					}
 					if (cell.getFormula() != null) {
 						cellStyle = "notchange";
 					}
 %> <s:set var="cellStyle"><%=cellStyle%></s:set> <s:textfield
								value="%{#cellValue}" title="%{#formula}"
								cssClass="%{#cellStyle}" name="cell_Row%{#row}_Col%{#col}"
								id="cell_Row%{#row}_Col%{#col}" size="15" theme="simple"
								readonly="%{#readonly}" onkeypress="return isAmountUnit(event)"
								onchange="changeValue('cell_Row%{#row}_Col%{#col}', this, 'cellCopy_Row%{#row}_Col%{#col}',%{collection.size()},%{collection.getRow(1).size()-1});" />
							<s:hidden name="cellCopy_Row%{#row}_Col%{#col}"
								id="cellCopy_Row%{#row}_Col%{#col}" value="%{#cellValue}" />
						</td>
						<%
							}
										colC++;
									}
						%>
					</tr>
					<%
						rowC++;
							}
							rowC--;
							colC--;
					%>
					<tr>
						<th
							style="border: double; border-color: #F18800; text-align: center; padding-left: 10px;"
							bgcolor="#E6F0FA" colspan="<%=collection.getRow(0).size()%>"
							height="40px;"><s:if test="#bReadOnly == 'false'">
								<input type="button" class="button btn-large-green"
									value="Внести изменения" onclick="sendFormBP('doUpdateBP');" />
							</s:if> <input type="hidden" name="rowspan" value=<%=rowSpan%> /> <input
							type="hidden" name="BPM_Form_ID"
							value=<%=fBP.getBPM_Form_ID()%> /> <input type="hidden"
							name="BPM_Project_ID" value=<%=fBP.getBPM_Project_ID()%> /> <input
							type="hidden" name="AD_Org_ID" value=<%=fBP.getAD_Org_ID()%> />
							<input type="hidden" name="AD_User_ID"
							value=<%=fBP.getAD_User_ID()%> /></th>
					</tr>
				</table>
			</s:form>
	</div>

	<script type="text/javascript">
			countRow = <%=rowC%>
			countCol = <%=colC%>
  var c = new Array();
  c = document.getElementsByTagName('input');
  for (var i = 0; i < c.length; i++)
  {
    if (c[i].type == 'checkbox')
    {
      c[i].checked = true;
      c[i].onchange(c[i]);	
    }
  }
	</script>
</body>
</html>